package fr.jade.service.fm.reactor;

import java.util.Set;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalContentException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;

import fr.jade.base.JadeBaseComponent;
import fr.jade.reflex.util.Reflex;
import fr.jade.util.FractalNavigator;
import fr.jade.util.FractalUtil;
import fr.jade.util.JadeException;

public class BasicCleaner extends JadeBaseComponent implements Cleaner {

  public void execute(Set<Component> failedComponents_M, Set<Component> impactedComponents_M, Component failedNode_M) throws JadeException {
    log("Basic Cleaner");    
    // delete failedNode and failedComponents from the rest of the architecture
    try{
      for (Component father_M : FractalUtil.getOptSuperController(failedNode_M).getFcSuperComponents()){
        try{
          log(FractalNavigator.getName(father_M) + " > " + FractalNavigator.getName(failedNode_M));
          Reflex.getContentNotificationController(father_M).rawRemoveFcSubComponentNotification(failedNode_M);
          Reflex.getContentNotificationController(Reflex.getDualComponent(father_M)).rawRemoveFcSubComponentNotification(Reflex.getDualComponent(failedNode_M));
        }catch (IllegalContentException e) {
          throw new JadeException("Exception during cleaning", e);
        } catch (IllegalLifeCycleException e) {
          throw new JadeException("Exception during cleaning", e);
        }
      }
    }catch(NoSuchInterfaceException e){
      throw new JadeException("Exception during cleaning", e);
    } 
  }

}
